import tip from '../utils/tip';
// let urlBase = 'https://property.jiabaimao.com/property';
// let urlBase = 'http://172.168.21.33:2536/property';
// let urlBase = 'http://192.168.0.23:2536/property';
// let urlBase = 'https://property.jiabaimao.com/propertytest';
const wxLogin = function (params,fun,header) {request(urlBase+'/owner/jscode2session',params,fun,header)};//微信登陆换取sesson
export function request(url,postData, doSuccess, header,WAY, doComplete,) {
    tip.loading();
    let header1 = {};
    let tway = 'GET';
    if(header){
        header1 = header
    }
    if(WAY&&WAY=="POST"){
      tway = "POST"
    }
    wx.request({
        url: url,
        data: postData,
        header:header1,
        method: tway,
        success: function (res) {
            tip.loaded();
            if(res.status===10){
                wx.removeStorageSync("token");
                wx.removeStorageSync("sessionid");
                if(url=="/owner/wechat/login" ||url=="/owner/jscode2session"){

                }else{
                    againLogin();
                }

                return false;
            }
            if (typeof doSuccess == "function") {
                doSuccess(res);
            }

        },
        fail: function (res) {
            tip.loaded();
            tip.error("网络错误！")
        },
        complete: function (res) {
            if(res.statusCode=="502" || res.statusCode=="500"){
                tip.error("服务器错误!")
            }
          if(res.statusCode=="401" ){
            wx.removeStorageSync("userDetail");
            tip.error("登陆信息失效！");
            wx.navigateTo({
              url: '/pages/login/main',
            })
            return false
          }
            if (typeof doComplete == "function") {
                doComplete();
            }
        }
    });
}

function againLogin() {
    wx.getUserInfo({
        withCredentials: true,
        success: res => {
            // 可以将 res 发送给后台解码出 unionId
            // this.globalData.userInfo = res.userInfo
            wx.setStorageSync('USER_INFO', res);
            // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
            // 所以此处加入 callback 以防止这种情况
            if(res.errMsg=="getUserInfo:ok"){
                if(!wx.getStorageSync("sessionid")||wx.getStorageSync("sessionid")==""){
                    wx.login({
                        success: codeRes => {
                            let para = {
                                jscode:codeRes.code,
                                iv:wx.getStorageSync('USER_INFO').iv,
                                encryptedData:wx.getStorageSync('USER_INFO').encryptedData
                            };
                            wxLogin(para,function (res) {
                                wx.setStorageSync("sessionid", res.header["Set-Cookie"]);
                                wx.navigateTo({
                                    url: '/pages/login/login',
                                })
                            });

                        }
                    });
                }
            }
        },
        fail:function(){
            wx.showModal({
                title: '警告',
                content: '您点击了拒绝授权,小程序将无法正常运行,请在设置界面勾选【允许使用我的用户信息】继续。',
                success:function(res){
                    if (res.confirm){
                        wx.openSetting({
                            success: (res) => {
                                if (res.authSetting["scope.userInfo"]){////如果用户重新同意了授权登录
                                }
                            },fail:function(res){

                            }
                        })

                    }
                }
            })
        }
    })
}
